//
//  FTextFiled.swift
//  jocool
//
//  Created by tong on 16/6/11.
//  Copyright © 2016年 zhuxietong. All rights reserved.
//

import UIKit
import Eelay



open class FTextField: FieldView, UITextFieldDelegate
{
    
    public let valueT = UITextField()
    override open func addLayoutRules() {
        self.eelay = [
            [valueT,[ee.T.L.B.R]]
        ]
    }
    
    open override func didLayoutRules() {
        valueT.delegate = self
        valueT.textColor = UIColor(shex: "#333333")
        valueT.font = UIFont.systemFont(ofSize: 15)
        
        valueT.returnKeyType = .done
        NotificationCenter.default.addObserver(self, selector: #selector(inputchange), name: UITextField.textDidChangeNotification, object: valueT)
    }
 

    
    @objc open func inputchange() {
        if let txt = valueT.text
        {
            
            //            var str = txt + string
            let str = txt
//            let whitespace = CharacterSet.whitespacesAndNewlines
//
//            var tempArray = str.components(separatedBy: whitespace)
//            tempArray = tempArray.filter{
//                $0 != ""
//            }
//            str = tempArray.joined(separator: "")
            self.save(str)
        }
    }

    func updateValue() {
        
        co_delay(delay: 0.01) {[weak self] in
            guard let `self` = self else {return}
            let t = self.valueT.text ?? ""
//            if t.hasSuffix("."){
//                t = t.replacingFirst(matching: ".$", with: "")
//            }
            
            print("__sssssdsds____S",t)
            self.save(t)

//            self.changePrice(t.CGFloatValue)
        }
    }
    
    public var inputType = "normal"{
        didSet{
            switch inputType {
            case "price":
                valueT.keyboardType = .decimalPad
            default:
                valueT.keyboardType = .default
            }
        }
    }
    public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        updateValue()

        if inputType == "normal"{
            return true
        }
        if inputType == "price"{
            guard var text = textField.text else {
                return true
            }
            if string == "-"{
                return false
            }

            if string.len < 1{
                updateValue()
                return true
            }
            
            if range.location <= text.count{
                let index = text.index(text.startIndex, offsetBy: range.location)
                text.insert(contentsOf: string, at: index)
            }
            if text.hasSuffix("."){
                return true
            }
            if text.mach("^\\d+(\\.\\d{1,4})?$"){
                return true
            }
    //        if let _ = Regex("\\d+\\.\\d{1,10}$").firstMatch(in: text){
    //            updatePrice()
    //            return true
    //        }
            return false
        }
        return true
        
        
       
       
    }
    open func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.save(textField.text ?? "")
        textField.resignFirstResponder()
        return true
    }
    
    
    open override func get_string_value() -> String {
        
        mo.model.setObject(self.valueT.text!, forKey: "value" as NSCopying)
        return mo.model["value",""].escapeHeadTailSpace()
    }

    open override func mo_model_reload() {
        super.mo_model_reload()

        valueT.text = mo.model[FK.value,""]
        valueT.ui.font(font:valueFont).text(color: valueColor)
        valueT.placeholder = place_holder
        valueT.setPlaceholder(info: (place_holder,holderColor,holderFont))
        inputType = mo.model["inputType","normal"]
        
        switch inputType {
        case "price":
            valueT.keyboardType = .decimalPad
        default:
            valueT.keyboardType = .default
        }
        if mo.model[FK.isSecureTextEntry,FK.NO] == FK.YES
        {
            valueT.isSecureTextEntry = true
        }
        else{
            valueT.isSecureTextEntry = false
        }

        
        let value = mo.model[FK.value,""]
        if value == ""
        {
            let defaultV = mo.model["defaultV",""]
            if defaultV != ""
            {
                mo.model.setObject(defaultV, forKey: FK.value as NSCopying)
                self.mo_model_reload()
            }
            
        }
    
        if mo.model[FK.editble,"YES"] == "NO"
        {
            valueT.isUserInteractionEnabled = false
        }
        else
        {
            valueT.isUserInteractionEnabled = true
        }
        
    }

    deinit{
        NotificationCenter.default.removeObserver(self, name: UITextField.textDidChangeNotification, object: valueT)
    }
    
  
}
